# Makefile for LVGL Car Speedometer
# 
# 用于编译基于LVGL的汽车时速表的Makefile

# 编译器设置
CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -O2 -g
LDFLAGS = -lm

# 检测系统
UNAME_S := $(shell uname -s 2>/dev/null || echo "Windows")

# LVGL相关配置
LVGL_DIR = ./lvgl
LVGL_DRIVERS_DIR = ./lv_drivers

# 包含路径
INCLUDES = -I. -I$(LVGL_DIR) -I$(LVGL_DRIVERS_DIR)

# 源文件
MAIN_SRC = speedometer.c
TARGET = speedometer

# LVGL源文件（如果本地编译）
LVGL_CORE_SRC = $(shell find $(LVGL_DIR)/src -name "*.c" 2>/dev/null || echo "")
LVGL_DRIVER_SRC = $(shell find $(LVGL_DRIVERS_DIR) -name "*.c" 2>/dev/null || echo "")

# 对象文件目录
OBJDIR = obj
OBJECTS = $(OBJDIR)/speedometer.o

# 检测SDL2
SDL2_CFLAGS := $(shell pkg-config --cflags sdl2 2>/dev/null || echo "-I/usr/include/SDL2")
SDL2_LIBS := $(shell pkg-config --libs sdl2 2>/dev/null || echo "-lSDL2")

# 检测LVGL库
LVGL_LIBS := $(shell pkg-config --libs lvgl 2>/dev/null || echo "")
LVGL_CFLAGS := $(shell pkg-config --cflags lvgl 2>/dev/null || echo "")

# 如果找到系统LVGL库
ifneq ($(LVGL_LIBS),)
    CFLAGS += $(LVGL_CFLAGS) -DUSE_SDL=0 -D_GNU_SOURCE
    LDFLAGS += $(LVGL_LIBS) -lm
    USE_SYSTEM_LVGL = 1
else
    # 使用本地LVGL源码（不包括lv_drivers）
    ifneq ($(LVGL_CORE_SRC),)
        LVGL_OBJECTS = $(LVGL_CORE_SRC:%.c=$(OBJDIR)/%.o)
        OBJECTS += $(LVGL_OBJECTS)
        CFLAGS += -DUSE_SDL=0 -D_GNU_SOURCE
        LDFLAGS += -lm
        USE_LOCAL_LVGL = 1
    else
        # 简单链接模式
        CFLAGS += -DUSE_SDL=0 -D_GNU_SOURCE
        LDFLAGS += -llvgl -lm
    endif
endif

# 默认目标
.PHONY: all
all: create-config check-deps $(TARGET)

# 检查依赖
.PHONY: check-deps
check-deps:
	@echo "检查依赖库..."
	@echo "编译选项: $(CFLAGS)"
	@echo "链接选项: $(LDFLAGS)"
	@echo ""
	@echo "ℹ️  注意: 该版本为简化模式，不依赖SDL"
	@echo "    程序将在控制台显示仪表盘数据"
	@echo ""
ifdef USE_SYSTEM_LVGL
	@echo "✅ 使用系统LVGL库"
else ifdef USE_LOCAL_LVGL
	@echo "⚠️  使用本地LVGL源码"
	@if [ ! -d "$(LVGL_DIR)" ]; then \
		echo "❌ 错误: 未找到LVGL源码目录 $(LVGL_DIR)"; \
		echo "请执行: make setup-lvgl"; \
		exit 1; \
	fi
else
	@echo "⚠️  尝试链接系统LVGL库"
endif
	@echo "✅ 依赖检查完成"

# 创建目录
$(OBJDIR):
	mkdir -p $(OBJDIR)
	mkdir -p $(OBJDIR)/$(LVGL_DIR)/src/core
	mkdir -p $(OBJDIR)/$(LVGL_DIR)/src/draw
	mkdir -p $(OBJDIR)/$(LVGL_DIR)/src/font
	mkdir -p $(OBJDIR)/$(LVGL_DIR)/src/hal
	mkdir -p $(OBJDIR)/$(LVGL_DIR)/src/misc
	mkdir -p $(OBJDIR)/$(LVGL_DIR)/src/widgets

# 编译目标程序
$(TARGET): $(OBJECTS)
	@echo "链接 $(TARGET)..."
	$(CC) $(OBJECTS) -o $@ $(LDFLAGS)
	@echo "✅ 编译完成: $(TARGET)"

# 编译主程序
$(OBJDIR)/speedometer.o: speedometer.c | $(OBJDIR)
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

# 编译LVGL源文件
ifdef USE_LOCAL_LVGL
$(OBJDIR)/%.o: %.c | $(OBJDIR)
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
endif

# 设置LVGL环境
.PHONY: setup-lvgl
setup-lvgl:
	@echo "设置LVGL开发环境（简化模式）..."
	@if [ ! -d "$(LVGL_DIR)" ]; then \
		echo "下载LVGL..."; \
		git clone --depth 1 https://github.com/lvgl/lvgl.git $(LVGL_DIR); \
	fi
	@echo "ℹ️  注意: 简化模式不需要lv_drivers"
	@if [ ! -f "lv_conf.h" ]; then \
		echo "创建LVGL配置文件..."; \
		make create-config; \
	fi
	@echo "✅ LVGL环境设置完成"

# 创建基本配置文件
.PHONY: create-config
create-config:
	@echo "创建基本配置文件..."
	@if [ ! -f "lv_conf.h" ]; then \
		echo "创建 lv_conf.h..."; \
		echo '#ifndef LV_CONF_H' > lv_conf.h; \
		echo '#define LV_CONF_H' >> lv_conf.h; \
		echo '' >> lv_conf.h; \
		echo '#define LV_COLOR_DEPTH 32' >> lv_conf.h; \
		echo '#define LV_MEM_SIZE (128U * 1024U)' >> lv_conf.h; \
		echo '#define LV_USE_LOG 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_14 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_16 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_18 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_20 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_22 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_24 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_26 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_28 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_30 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_32 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_34 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_36 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_38 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_40 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_42 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_44 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_46 1' >> lv_conf.h; \
		echo '#define LV_FONT_MONTSERRAT_48 1' >> lv_conf.h; \
		echo '#define LV_USE_ARC 1' >> lv_conf.h; \
		echo '#define LV_USE_BAR 1' >> lv_conf.h; \
		echo '#define LV_USE_LED 1' >> lv_conf.h; \
		echo '#define LV_USE_LINE 1' >> lv_conf.h; \
		echo '#define LV_USE_LABEL 1' >> lv_conf.h; \
		echo '' >> lv_conf.h; \
		echo '#endif' >> lv_conf.h; \
	fi
	@if [ ! -f "lv_drv_conf.h" ]; then \
		echo "创建 lv_drv_conf.h..."; \
		echo '#ifndef LV_DRV_CONF_H' > lv_drv_conf.h; \
		echo '#define LV_DRV_CONF_H' >> lv_drv_conf.h; \
		echo '' >> lv_drv_conf.h; \
		echo '#define USE_MONITOR 1' >> lv_drv_conf.h; \
		echo '#define USE_MOUSE 1' >> lv_drv_conf.h; \
		echo '#define USE_KEYBOARD 1' >> lv_drv_conf.h; \
		echo '#define MONITOR_HOR_RES 800' >> lv_drv_conf.h; \
		echo '#define MONITOR_VER_RES 600' >> lv_drv_conf.h; \
		echo '' >> lv_drv_conf.h; \
		echo '#endif' >> lv_drv_conf.h; \
	fi

# 清理
.PHONY: clean
clean:
	rm -rf $(OBJDIR) $(TARGET) $(TARGET).exe
	@echo "清理完成"

# 深度清理（包括LVGL）
.PHONY: distclean
distclean: clean
	rm -rf $(LVGL_DIR) $(LVGL_DRIVERS_DIR) lv_conf.h lv_drv_conf.h
	@echo "深度清理完成"

# 安装依赖
.PHONY: install-deps
install-deps:
	@echo "安装依赖库（简化模式）..."
	@if command -v apt-get >/dev/null 2>&1; then \
		echo "检测到Debian/Ubuntu系统"; \
		sudo apt-get update; \
		sudo apt-get install -y pkg-config build-essential git; \
	elif command -v yum >/dev/null 2>&1; then \
		echo "检测到CentOS/RHEL系统"; \
		sudo yum install -y pkgconfig gcc make git; \
	elif command -v dnf >/dev/null 2>&1; then \
		echo "检测到Fedora系统"; \
		sudo dnf install -y pkgconfig gcc make git; \
	elif command -v brew >/dev/null 2>&1; then \
		echo "检测到macOS系统"; \
		brew install pkg-config git; \
	else \
		echo "无法自动安装，请手动安装:"; \
		echo "- 基本开发工具 (gcc, make)"; \
		echo "- pkg-config"; \
		echo "- Git（用于下载LVGL）"; \
	fi
	@echo "ℹ️  注意: 简化模式不需要SDL2库"

# 运行程序
.PHONY: run
run: $(TARGET)
	@echo "启动汽车仪表盘..."
	./$(TARGET)

# 测试模式
.PHONY: test
test: $(TARGET)
	@echo "测试模式（初始速度60km/h）..."
	./$(TARGET) -s 60

# 运行功能测试
.PHONY: test-function
test-function: $(TARGET)
	@if [ -f "test.sh" ]; then \
		chmod +x test.sh; \
		./test.sh; \
	else \
		echo "测试脚本不存在"; \
	fi

# 快速修复
.PHONY: fix
fix:
	@if [ -f "fix_build.sh" ]; then \
		chmod +x fix_build.sh; \
		./fix_build.sh; \
	else \
		echo "修复脚本不存在，尝试基本修复..."; \
		make clean; \
		make create-config; \
		make; \
	fi

# 修复字体问题
.PHONY: fix-fonts
fix-fonts:
	@if [ -f "fix_fonts.sh" ]; then \
		chmod +x fix_fonts.sh; \
		./fix_fonts.sh; \
	else \
		echo "手动修复字体问题..."; \
		sed -i.bak 's/lv_font_montserrat_[0-9][0-9]*/lv_font_montserrat_14/g' speedometer.c; \
		make clean && make; \
	fi

# 调试模式
.PHONY: debug
debug: CFLAGS += -DDEBUG -g3 -O0
debug: clean $(TARGET)
	@echo "调试版本编译完成"

# 发布模式
.PHONY: release
release: CFLAGS += -DNDEBUG -O3
release: clean $(TARGET)
	strip $(TARGET) 2>/dev/null || true
	@echo "发布版本编译完成"

# 帮助信息
.PHONY: help
help:
	@echo "可用的make目标:"
	@echo "  all          - 编译程序 (默认)"
	@echo "  check-deps   - 检查依赖库"
	@echo "  setup-lvgl   - 设置LVGL开发环境"
	@echo "  create-config- 创建基本配置文件"
	@echo "  install-deps - 安装系统依赖"
	@echo "  clean        - 清理编译产物"
	@echo "  distclean    - 深度清理（包括LVGL）"
	@echo "  debug        - 编译调试版本"
	@echo "  release      - 编译发布版本"
	@echo "  run          - 运行程序"
	@echo "  test         - 测试模式运行"
	@echo "  test-function- 运行功能测试"
	@echo "  fix          - 快速修复编译问题"
	@echo "  fix-fonts    - 修复字体相关问题"
	@echo "  help         - 显示此帮助信息"
	@echo "  info         - 显示编译信息"

# 显示信息
.PHONY: info
info:
	@echo "项目信息:"
	@echo "  编译器: $(CC)"
	@echo "  编译选项: $(CFLAGS)"
	@echo "  链接选项: $(LDFLAGS)"
	@echo "  目标程序: $(TARGET)"
	@echo "  系统: $(UNAME_S)"
ifdef USE_SYSTEM_LVGL
	@echo "  LVGL: 系统库"
else ifdef USE_LOCAL_LVGL
	@echo "  LVGL: 本地源码"
else
	@echo "  LVGL: 尝试系统库"
endif